{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "86c445c3-deb3-48e2-b30f-8d2d3d8957e5",
   "metadata": {},
   "source": [
    "# Install and require Noj"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a63203c4-de7b-4481-be45-8d704963acfb",
   "metadata": {},
   "outputs": [],
   "source": [
    "(require '[clojupyter.misc.helper :as helper])\n",
    "(def _ (helper/add-dependencies '[org.scicloj/noj \"2-beta5.1\"]))\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b5fba2e-1f23-4079-b488-3ab56c425ecf",
   "metadata": {},
   "outputs": [],
   "source": [
    ";(def _ (helper/add-dependencies '[org.scicloj/kindly  \"4-beta14\"]))\n",
    ";(def _ (helper/add-dependencies '[org.scicloj/kindly-advice  \"1-beta12\"]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0d75d7b-287f-4e53-8b0b-f80d6cbef7be",
   "metadata": {},
   "outputs": [],
   "source": [
    "(require '[scicloj.kindly.v4.kind :as kind]\n",
    "         '[tablecloth.api :as tc])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8b8135d-b113-474c-be68-609af6154335",
   "metadata": {},
   "source": [
    "# Plain values\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50e165a9-58f3-42bc-9eed-abd3dfc9ef9b",
   "metadata": {},
   "outputs": [],
   "source": [
    "(+  4 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7419ac36-ebc1-4c20-89ac-a8fcf86d24bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "(str \"abcd\" \"efgh\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d2cbe21-adbd-4300-b216-52a6f794e3a8",
   "metadata": {},
   "source": [
    "# Plain data structures"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "081456b5-5eaf-49e4-bf0b-9b60d5ca3d94",
   "metadata": {},
   "outputs": [],
   "source": [
    "(list 1 \"A\" :B 'C)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3580c404-6f30-4f14-bd41-738d953e5d64",
   "metadata": {},
   "outputs": [],
   "source": [
    "(range 9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84aa9d3c-9689-451b-81cd-0ef160ccb831",
   "metadata": {},
   "outputs": [],
   "source": [
    "[1 \"A\" :B 'C]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6d43e94-867a-4c11-902b-6331af6c6b5f",
   "metadata": {},
   "outputs": [],
   "source": [
    "#{1 \"A\" :B 'C}\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64fdd8b2-cc58-4f42-92ea-f8b63c65e0da",
   "metadata": {},
   "outputs": [],
   "source": [
    "#{1 \"A\" :B 'C}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d34d1184-62fb-49d4-b59d-d4b616f3e150",
   "metadata": {},
   "outputs": [],
   "source": [
    "(def people-as-maps\n",
    "  (->> (range 29)\n",
    "       (mapv (fn [i]\n",
    "               {:preferred-language ([\"clojure\" \"clojurescript\" \"babashka\"]\n",
    "                                     (rand-int 3))\n",
    "                :age (rand-int 100)}))))\n",
    "(take 5 people-as-maps)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45d48cb9-a611-462d-8567-7c873a514efb",
   "metadata": {},
   "outputs": [],
   "source": [
    "(def people-as-vectors\n",
    "  (->> people-as-maps\n",
    "       (mapv (juxt :preferred-language :age))))\n",
    "(take 5 people-as-vectors)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf5f314b-3674-40a1-83ac-14c01ea723bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "[(kind/hiccup\n",
    "  [:div {:style\n",
    "         {:background-color \"floralwhite\"}}\n",
    "   [:p \"hello\"]])\n",
    " (kind/md\n",
    "  \"hello *hello* **hello**\")\n",
    " (kind/code\n",
    "  \"(defn f [x] (+  x 9))\")]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5f6c6fb-34af-4df1-9136-9e6355f80637",
   "metadata": {},
   "outputs": [],
   "source": [
    "{:x  (kind/md\n",
    "      \"**hello**\")\n",
    " (kind/md\n",
    "  \"**hello**\") :x}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d23eeecf-7603-4010-93d9-05b401f52c30",
   "metadata": {},
   "outputs": [],
   "source": [
    "{:vector-of-numbers [2 9 -1]\n",
    " :vector-of-different-things [\"hi\"\n",
    "                              (kind/hiccup\n",
    "                               [:big [:big \"hello\"]])]\n",
    " :map-of-different-things {:markdown (kind/md [\"*hi*, **hi**\"])\n",
    "                           :number 9999}\n",
    " :hiccup (kind/hiccup\n",
    "          [:big [:big \"bye\"]])\n",
    " :dataset (tc/dataset {:x (range 3)\n",
    "                       :y [:A :B :C]})}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24345676-8b99-4da9-bcca-58f989217b4d",
   "metadata": {},
   "source": [
    "# :kind/seq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fba7c849-06e6-45cf-afe5-092f0d83672f",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/seq [1 2 3 4])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "409a4c9a-81b7-4d71-b0ed-e25f0198a00a",
   "metadata": {},
   "source": [
    "# :kind/vector    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67ddafa2-153a-4729-9672-9b36567ebc5a",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/vector [1 2 3])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6fb6d8b-061b-49da-9cdb-e9602c0092ab",
   "metadata": {},
   "source": [
    "# :kind/map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96cc1ed0-07de-48e4-9337-0113349f9d46",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/map {1 \"A\" :B 'C})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21f71d6a-3995-4294-8456-c408cc856863",
   "metadata": {},
   "source": [
    "# :kind/set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1537d9f4-a59e-47c9-92fd-78e6d323d146",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/set #{1 2 3})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3cd85893-d7bf-4c4f-98b8-d3038919b182",
   "metadata": {},
   "source": [
    "# :kind/hidden\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df678d5f-7a06-40ef-a931-320d80a73d73",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/hidden\n",
    " {:x 9})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "963e0c67-8a77-4c18-93eb-113d72148ad4",
   "metadata": {},
   "source": [
    "# :kind/md\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99a1ace1-96b0-4d13-acd2-699bd305cbcd",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/md\n",
    "     \"\n",
    "    * This is [markdown](https://www.markdownguide.org/).\n",
    "      * *Isn't it??*\n",
    "    * Here is **some more** markdown.\"\n",
    "      \n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7471e6d-54c0-4c01-89a7-5f1241928d68",
   "metadata": {},
   "outputs": [],
   "source": [
    "(def m\n",
    "    (kind/md\n",
    "\"\n",
    "* This is [markdown](https://www.markdownguide.org/).\n",
    "    * *Isn't it??*\n",
    "    * Here is **some more** markdown.\n",
    "\"\n",
    "    ))\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5236b2cf-5651-44f7-ba12-28246cb5465d",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/md\n",
    " \"If $x$ equals 9, then $$x^2+9=90$$\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f261147-bbd5-4818-8095-c028bcafcd95",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/md \"*123*\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88b8e338-1341-4897-b48e-0586be4dca9b",
   "metadata": {},
   "outputs": [],
   "source": [
    "(do (def m (kind/md \"**123**\")) m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e35677e0-e07e-42da-a451-00d5e9e2e173",
   "metadata": {},
   "outputs": [],
   "source": [
    "(def m (kind/md \"_123_\")) \n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f538aeb2-ee5e-477e-b63b-bd99a5df1617",
   "metadata": {},
   "source": [
    "# :kind/tex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46453da1-3da4-4b92-981f-dc64bea2ae56",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "(def tex\n",
    " (kind/tex \"x^2=\\\\alpha\"))\n",
    "tex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04f3a363-29d9-4e86-8c88-20dd2f83b854",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/tex \"\\\\sum_{i=1}^\\\\infty\\\\frac{1}{n^2}\n",
    "=\\\\frac{\\\\pi^2}{6}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42b30f2c-b1bf-44d6-8540-c3becbd144b0",
   "metadata": {},
   "source": [
    "# :kind/code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b86ec76-81e5-4636-b840-6598b4c8f664",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/code\n",
    " \"(defn f [x] {:y (+  x 9)})\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3dbf044a-79c9-4a28-bd07-304aad47e191",
   "metadata": {},
   "source": [
    "# :kind/edn"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68702741-bc6a-4cef-91ab-ec705e0d9eb3",
   "metadata": {},
   "source": [
    "# :kind/hiccup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f8633d9-bede-431d-a60b-868ea28645d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "(def hello-hiccup\n",
    "  (kind/hiccup\n",
    "   [:ul\n",
    "    [:li [:p \"hi\"]]\n",
    "    [:li [:big\n",
    "          [:big\n",
    "           [:p {:style\n",
    "                ;; https://www.htmlcsscolor.com/hex/7F5F3F\n",
    "                {:color \"#7F5F3F\"}}\n",
    "            \"hello\"]]]]]))\n",
    "hello-hiccup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "955b87a1-b514-4947-81ea-ca0adeacf323",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/hiccup\n",
    " [:div {:style\n",
    "        {:background-color \"#eeddee\"\n",
    "         :border-style \"solid\"}}\n",
    "  [:p {:style {:background-color \"#ccddcc\"\n",
    "               :border-style \"solid\"}}\n",
    "   \"hello\"]\n",
    "  (kind/md\n",
    "   \"hello *hello* **hello**\")\n",
    "  (kind/code\n",
    "   \"(defn f [x] (+  x 9))\")])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20467130-c4d3-4269-b20d-2d944f27df49",
   "metadata": {},
   "source": [
    "# :Kind/reagent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63806703-141a-4eeb-b93c-0197c10954e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/reagent\n",
    " ['(fn [numbers]\n",
    "     [:p {:style {:background \"#d4ebe9\"}}\n",
    "      (pr-str (map inc numbers))])\n",
    "  (vec (range 10))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "afeb8199-843c-47aa-acff-6067999d28a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/scittle\n",
    " '(defn f [x]\n",
    "    (+ x 9)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a48e442f-aba7-48af-bbbf-f370dd3e06e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/reagent\n",
    " ['(fn []\n",
    "     [:p (f 20)])])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9c49203-e591-4936-9eb8-d49820736ead",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/reagent\n",
    " ['(fn [{:keys [spec0\n",
    "                transition\n",
    "                time-for-transition]}]\n",
    "     (let [*spec (reagent.core/atom spec0)]\n",
    "       (fn []\n",
    "         ^{:key @*spec}\n",
    "         [:div\n",
    "          [:div {:style {:height \"400px\"}\n",
    "                 :ref (fn [el]\n",
    "                        (when el\n",
    "                          (let [chart (.init js/echarts el)]\n",
    "                            (.setOption chart (clj->js @*spec)))))}]\n",
    "          (js/setInterval #(swap! *spec transition) time-for-transition)\n",
    "          ;; Include this to force component update:\n",
    "          [:p {:style {:display :none}}\n",
    "           (hash @*spec)]])))\n",
    "  {:spec0 {:title {:text \"Echarts Example\"}\n",
    "           :tooltip {}\n",
    "           :legend {:data [\"sales\"]}\n",
    "           :xAxis {:data [\"Shirts\", \"Cardigans\", \"Chiffons\",\n",
    "                          \"Pants\", \"Heels\", \"Socks\"]}\n",
    "           :yAxis {}\n",
    "           :series [{:name \"sales\"\n",
    "                     :type \"bar\"\n",
    "                     :data [5 20 36\n",
    "                            10 10 20]}]}\n",
    "   :transition '(fn [spec]\n",
    "                  (update-in spec\n",
    "                             [:series 0 :data]\n",
    "                             (partial map #(+ %\n",
    "                                              (rand-int 10)\n",
    "                                              -5))))\n",
    "   :time-for-transition 1000}]\n",
    " {:html/deps [:kind/echarts]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e74f15e-a1b1-401f-89f7-94cf2c5e709e",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/reagent\n",
    " ['(fn []\n",
    "     [:div {:style {:height \"200px\"}\n",
    "            :ref (fn [el]\n",
    "                   (let [m (-> js/L\n",
    "                               (.map el)\n",
    "                               (.setView (clj->js [51.505 -0.09])\n",
    "                                         13))]\n",
    "                     (-> js/L\n",
    "                         .-tileLayer\n",
    "                         (.provider \"OpenStreetMap.Mapnik\")\n",
    "                         (.addTo m))\n",
    "                     (-> js/L\n",
    "                         (.marker (clj->js [51.5 -0.09]))\n",
    "                         (.addTo m)\n",
    "                         (.bindPopup \"A pretty CSS popup.<br> Easily customizable.\")\n",
    "                         (.openPopup))))}])]\n",
    " ;; Note we need to mention the dependency:\n",
    " {:html/deps [:leaflet]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c904f7d-3def-4687-b60d-ed0aeedd178f",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/reagent\n",
    " ['(fn []\n",
    "     (let [*click-count (reagent.core/atom 0)]\n",
    "       (fn []\n",
    "         [:div\n",
    "          \"The atom \" [:code \"*click-count\"] \" has value: \"\n",
    "          @*click-count \". \"\n",
    "          [:input {:type \"button\" :value \"Click me!\"\n",
    "                   :on-click #(swap! *click-count inc)}]])))])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1dcd2d62-56f9-42af-a128-71602512b7c6",
   "metadata": {},
   "source": [
    "# :kind/html\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35e39ec3-e846-4c50-a685-2125f0e045df",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/html\n",
    " \"<div style='height:40px; width:40px; background:purple'></div> \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "90e81109-3aba-4e9c-bc78-5a9d8c820186",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/html\n",
    " \"\n",
    "<svg height=100 width=100>\n",
    "<circle cx=50 cy=50 r=40 stroke='purple' stroke-width=3 fill='floralwhite' />\n",
    "</svg> \")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86e3c44b-d369-4f8f-9c9e-2c8ba98bf2ef",
   "metadata": {},
   "source": [
    "# :kind/vega"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b0dd269-99e2-4670-ab7d-0edac9ef9a7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "(repeat 4\n",
    "(kind/vega\n",
    " {:$schema \"https://vega.github.io/schema/vega/v5.json\"\n",
    "  :width 400\n",
    "  :height 200\n",
    "  :padding 5\n",
    "  :data {:name \"table\"\n",
    "         :values [{:category :A :amount 28}\n",
    "                  {:category :B :amount 55}\n",
    "                  {:category :C :amount 43}\n",
    "                  {:category :D :amount 91}\n",
    "                  {:category :E :amount 81}\n",
    "                  {:category :F :amount 53}\n",
    "                  {:category :G :amount 19}\n",
    "                  {:category :H :amount 87}]}\n",
    "  :signals [{:name :tooltip\n",
    "             :value {}\n",
    "             :on [{:events \"rect:mouseover\"\n",
    "                   :update :datum}\n",
    "                  {:events \"rect:mouseout\"\n",
    "                   :update \"{}\"}]}]\n",
    "  :scales [{:name :xscale\n",
    "            :type :band\n",
    "            :domain {:data :table\n",
    "                     :field :category}\n",
    "            :range :width\n",
    "            :padding 0.05\n",
    "            :round true}\n",
    "           {:name :yscale\n",
    "            :domain {:data :table\n",
    "                     :field :amount}\n",
    "            :nice true\n",
    "            :range :height}]\n",
    "  :axes [{:orient :bottom :scale :xscale}\n",
    "         {:orient :left :scale :yscale}]\n",
    "  :marks {:type :rect\n",
    "          :from {:data :table}\n",
    "          :encode {:enter {:x {:scale :xscale\n",
    "                               :field :category}\n",
    "                           :width {:scale :xscale\n",
    "                                   :band 1}\n",
    "                           :y {:scale :yscale\n",
    "                               :field :amount}\n",
    "                           :y2 {:scale :yscale\n",
    "                                :value 0}}\n",
    "                   :update {:fill\n",
    "                            {:value :steelblue}}\n",
    "                   :hover {:fill\n",
    "                           {:value :red}}}}}))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "098a6297-4128-4b76-85f2-e973d110330e",
   "metadata": {},
   "source": [
    "# :kind/vega-lite"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c7187ad-a722-4526-ad07-c0513365b80d",
   "metadata": {},
   "outputs": [],
   "source": [
    "(def vega-lite-plot\n",
    "  (kind/vega-lite\n",
    "   {:encoding\n",
    "    {:y {:field \"y\", :type \"quantitative\"},\n",
    "     :size {:value 400},\n",
    "     :x {:field \"x\", :type \"quantitative\"}},\n",
    "    :mark {:type \"circle\", :tooltip true},\n",
    "    :width 400,\n",
    "    :background \"floralwhite\",\n",
    "    :height 100,\n",
    "    :data {:values \"x,y\\n1,1\\n2,-4\\n3,9\\n\", :format {:type \"csv\"}}}))\n",
    "vega-lite-plot"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c06cc0d2-fb67-42a2-b046-5eecaf1b4be4",
   "metadata": {},
   "source": [
    "# :kind/cytoscape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b66b420-471c-4b38-b52b-4e3a85d83336",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/cytoscape\n",
    " {:elements {:nodes [{:data {:id \"a\" :parent \"b\"} :position {:x 215 :y 85}}\n",
    "                     {:data {:id \"b\"}}\n",
    "                     {:data {:id \"c\" :parent \"b\"} :position {:x 300 :y 85}}\n",
    "                     {:data {:id \"d\"} :position {:x 215 :y 175}}\n",
    "                     {:data {:id \"e\"}}\n",
    "                     {:data {:id \"f\" :parent \"e\"} :position {:x 300 :y 175}}]\n",
    "             :edges [{:data {:id \"ad\" :source \"a\" :target \"d\"}}\n",
    "                     {:data {:id \"eb\" :source \"e\" :target \"b\"}}]}\n",
    "  :style [{:selector \"node\"\n",
    "           :css {:content \"data(id)\"\n",
    "                 :text-valign \"center\"\n",
    "                 :text-halign \"center\"}}\n",
    "          {:selector \"parent\"\n",
    "           :css {:text-valign \"top\"\n",
    "                 :text-halign \"center\"}}\n",
    "          {:selector \"edge\"\n",
    "           :css {:curve-style \"bezier\"\n",
    "                 :target-arrow-shape \"triangle\"}}]\n",
    "  :layout {:name \"preset\"\n",
    "           :padding 5}})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82524346-edbf-4dd7-8abf-35ce22267482",
   "metadata": {},
   "source": [
    "# :kind/echarts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c1ca63f4-96d4-45d3-ac13-bb7c177cf76e",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/echarts\n",
    " {:title {:text \"Echarts Example\"}\n",
    "  :tooltip {}\n",
    "  :legend {:data [\"sales\"]}\n",
    "  :xAxis {:data [\"Shirts\", \"Cardigans\", \"Chiffons\",\n",
    "                 \"Pants\", \"Heels\", \"Socks\"]}\n",
    "  :yAxis {}\n",
    "  :series [{:name \"sales\"\n",
    "            :type \"bar\"\n",
    "            :data [5 20 36\n",
    "                   10 10 20]}]})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64aa2281-271e-4340-a017-24dc062e45fe",
   "metadata": {},
   "source": [
    "# :kind/plotly\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ef677b1-827b-4df9-8ae1-f79ca973f6d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "(def plotly-example \n",
    "    (let [n 20\n",
    "       walk (fn [bias]\n",
    "              (->> (repeatedly n #(-> (rand)\n",
    "                                      (- 0.5)\n",
    "                                      (+ bias)))\n",
    "                   (reductions +)))]\n",
    "   {:data [{:x (walk 1)\n",
    "            :y (walk -1)\n",
    "            :z (map #(* % %)\n",
    "                    (walk 2))\n",
    "            :type :scatter3d\n",
    "            :mode :lines+markers\n",
    "            :opacity 0.2\n",
    "            :line {:width 10}\n",
    "            :marker {:size 20\n",
    "                     :colorscale :Viridis}}]}))\n",
    "(kind/plotly plotly-example)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9541a8c3-d4d1-4f73-9256-9635d3ab5579",
   "metadata": {},
   "outputs": [],
   "source": [
    "(-> plotly-example\n",
    "    (kind/plotly {:style\n",
    "                  {:width \"100px\"\n",
    "                   :height \"100px\"}}))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d869334-4002-4419-91c9-d01d9205b4b8",
   "metadata": {},
   "source": [
    "# :kind/highcharts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb34c31d-c647-45e4-9565-d3841a310418",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/highcharts\n",
    " {:title {:text \"Line chart\"}\n",
    "  :subtitle {:text \"By Job Category\"}\n",
    "  :yAxis {:title {:text \"Number of Employees\"}}\n",
    "  :series [{:name \"Installation & Developers\"\n",
    "            :data [43934, 48656, 65165, 81827, 112143, 142383,\n",
    "                   171533, 165174, 155157, 161454, 154610]}\n",
    "\n",
    "           {:name \"Manufacturing\",\n",
    "            :data [24916, 37941, 29742, 29851, 32490, 30282,\n",
    "                   38121, 36885, 33726, 34243, 31050]}\n",
    "\n",
    "           {:name \"Sales & Distribution\",\n",
    "            :data [11744, 30000, 16005, 19771, 20185, 24377,\n",
    "                   32147, 30912, 29243, 29213, 25663]}\n",
    "\n",
    "           {:name \"Operations & Maintenance\",\n",
    "            :data [nil, nil, nil, nil, nil, nil, nil,\n",
    "                   nil, 11164, 11218, 10077]}\n",
    "\n",
    "           {:name \"Other\",\n",
    "            :data [21908, 5548, 8105, 11248, 8989, 11816, 18274,\n",
    "                   17300, 13053, 11906, 10073]}]\n",
    "\n",
    "  :xAxis {:accessibility {:rangeDescription \"Range: 2010 to 2020\"}}\n",
    "\n",
    "  :legend {:layout \"vertical\",\n",
    "           :align \"right\",\n",
    "           :verticalAlign \"middle\"}\n",
    "\n",
    "  :plotOptions {:series {:label {:connectorAllowed false},\n",
    "                         :pointStart 2010}}\n",
    "\n",
    "  :responsive {:rules [{:condition {:maxWidth 500},\n",
    "                        :chartOptions {:legend {:layout \"horizontal\",\n",
    "                                                :align \"center\",\n",
    "                                                :verticalAlign \"bottom\"}}}]}})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a193968-a3e9-4d4e-93eb-10731d6e18ee",
   "metadata": {},
   "source": [
    "# :kind/observable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "733df0d5-6c82-483b-8263-b216d5700009",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/observable\n",
    " \"athletes = FileAttachment('notebooks/datasets/athletes.csv').csv({typed: true})\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b7ee0167-003f-41f7-aaec-a1521b0b02c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/observable\n",
    " \"athletes\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c9fb0c42-e024-4803-b8e9-135d7831d41c",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/observable\n",
    " \"\n",
    "Plot.plot({\n",
    "  grid: true,\n",
    "  facet: {\n",
    "    data: athletes,\n",
    "    y: 'sex'\n",
    "  },\n",
    "  marks: [\n",
    "    Plot.rectY(\n",
    "      athletes,\n",
    "      Plot.binX({y: 'count'}, {x: 'weight', fill: 'sex'})\n",
    "    ),\n",
    "    Plot.ruleY([0])\n",
    "  ]\n",
    "})\n",
    "\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ce68a43-578d-4ee8-88da-0b7e46d6fded",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/observable\n",
    " \"population = FileAttachment('notebooks/datasets/population.json').json()\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80b6a58e-2460-4b25-937e-08c872e21ca0",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/observable\n",
    " \"population\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "297fa30e-be55-41cf-9276-e801439516e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/observable\n",
    " \" import { chart } with { population as data } from '@d3/zoomable-sunburst'\n",
    " chart\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f9845517-bb42-4d10-9251-36c3e2125e08",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/observable\n",
    " \"\n",
    "//| panel: input\n",
    "viewof bill_length_min = Inputs.range(\n",
    "                                      [32, 50],\n",
    "                                      {value: 35, step: 1, label: 'Bill length (min):'}\n",
    "                                      )\n",
    "viewof islands = Inputs.checkbox(\n",
    "                                 ['Torgersen', 'Biscoe', 'Dream'],\n",
    "                                 { value: ['Torgersen', 'Biscoe'],\n",
    "                                  label: 'Islands:'\n",
    "                                  }\n",
    "                                 )\n",
    "\n",
    "Plot.rectY(filtered,\n",
    "            Plot.binX(\n",
    "                      {y: 'count'},\n",
    "                      {x: 'body_mass_g', fill: 'species', thresholds: 20}\n",
    "                      ))\n",
    " .plot({\n",
    "        facet: {\n",
    "                data: filtered,\n",
    "                x: 'sex',\n",
    "                y: 'species',\n",
    "                marginRight: 80\n",
    "                },\n",
    "        marks: [\n",
    "                Plot.frame(),\n",
    "                ]\n",
    "        }\n",
    "       )\n",
    "Inputs.table(filtered)\n",
    "data = FileAttachment('notebooks/datasets/palmer-penguins.csv').csv({ typed: true })\n",
    "filtered = data.filter(function(penguin) {\n",
    "                                           return bill_length_min < penguin.bill_length_mm &&\n",
    "                                           islands.includes(penguin.island);\n",
    "                                           })\n",
    "\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01cfe99e-b8f6-427f-b67c-c27efa0f6590",
   "metadata": {},
   "source": [
    "# :kind/video"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2fc800e8-b8d7-4710-9fa5-e57cb44da6a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/video\n",
    " {:youtube-id \"DAQnvAgBma8\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76ced345-6347-4c24-9f92-44bf269d3b70",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/video\n",
    " {:youtube-id \"DAQnvAgBma8\"\n",
    "  :allowfullscreen false})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43f0a970-7e7c-42b2-96da-ff955e0a4d56",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/video\n",
    " {:youtube-id \"DAQnvAgBma8\"\n",
    "  :iframe-width 480\n",
    "  :iframe-height 270})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f87dcda4-60fe-4acc-85ca-a25866d8cb3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/video\n",
    " {:youtube-id \"DAQnvAgBma8\"\n",
    "  :embed-options {:mute 1\n",
    "                  :controls 0}})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6055f0d5-8218-400a-a618-f4939e4ab207",
   "metadata": {},
   "source": [
    "# :kind/image"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54f8f3a0-48e5-439a-b19a-78d2bf669cff",
   "metadata": {},
   "source": [
    "without \"kind\":"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9102fcf6-1ed9-4a0b-8996-0e1f25d0bf41",
   "metadata": {},
   "outputs": [],
   "source": [
    "(defonce tree-image\n",
    "  (->  \"https://upload.wikimedia.org/wikipedia/commons/e/eb/Ash_Tree_-_geograph.org.uk_-_590710.jpg\"\n",
    "       (java.net.URL.)\n",
    "       (javax.imageio.ImageIO/read)))\n",
    "tree-image"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57a6f3b2-82a5-48cb-8160-68ed2102cfe9",
   "metadata": {},
   "source": [
    "with \"kind\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee3691ce-1847-4cec-a8de-1896304218c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/image tree-image)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c874c2a-8bbe-4539-9edd-6333e4150d55",
   "metadata": {},
   "source": [
    "inside hiccup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d93fad46-1b57-4326-910b-264285773bf5",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/hiccup [:div.clay-limit-image-width\n",
    "                       tree-image])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bd235a10-cdfd-4ec6-8ed1-fa01de747f4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "[tree-image tree-image]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b70f27b-a3f5-4cd3-8e59-05f01087db9d",
   "metadata": {},
   "source": [
    "# :kind/dataset\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "54a42fae-d808-4b20-aa52-6a485417aed9",
   "metadata": {},
   "outputs": [],
   "source": [
    "(def squares-dataset\n",
    "  (-> {:x (range 25)}\n",
    "      tc/dataset\n",
    "      (tc/map-columns :y\n",
    "                      [:x]\n",
    "                      (fn [x]\n",
    "                        (* x x)))))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "737cc6a8-b53d-401e-88e4-fb16a15146a8",
   "metadata": {},
   "source": [
    "without \"kind\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0dc4986a-843b-4c05-9c5b-8b0b2945b3e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "(tc/dataset\n",
    " {:x [1 [2 3] 4]\n",
    "  :y [:A :B :C]})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52d7a8e7-78e6-46bd-a340-234a70db9d00",
   "metadata": {},
   "source": [
    "with kind"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f52217b1-e192-4e21-8c2c-68da0baf99fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/dataset\n",
    "(tc/dataset\n",
    " {:x [1 [2 3] 4]\n",
    "  :y [:A :B :C]}))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1182ca04-7b22-4ea5-b91c-120661c87142",
   "metadata": {},
   "source": [
    "# :kind/table "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9933fe6b-3eb4-48e8-876b-88ab727cdb38",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/table\n",
    " {:column-names [:preferred-language :age]\n",
    "  :row-vectors (take 5 people-as-vectors)})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f4781dd8-fb12-4546-a70d-e642f4c7176d",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/table\n",
    " {:row-vectors (take 5 people-as-vectors)})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc417d53-31fa-440e-b690-adf1302cd6a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/table\n",
    " {:row-maps (take 5 people-as-maps)})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84f78b8d-9e02-47e9-ace9-8b582b20e94a",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/table\n",
    " {:column-names [:preferred-language]\n",
    "  :row-maps (take 5 people-as-maps)})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "190b4070-d36d-4cb7-8914-0018649e563b",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/table (take 5 people-as-vectors))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3eac649f-82a4-4a32-803f-3cd23006a667",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/table (take 5 people-as-maps))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01910207-2b67-4151-909a-93f71f77b783",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/table {:x (range 6)\n",
    "             :y [:A :B :C :A :B :C]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0635afba-e0e9-4cd1-836a-571a32195bd8",
   "metadata": {},
   "outputs": [],
   "source": [
    "(def people-as-dataset\n",
    "  (tc/dataset people-as-maps))\n",
    "(-> people-as-dataset\n",
    "    kind/table)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cee19490-5063-46c4-8bc4-34868e4ec3f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "(-> people-as-dataset\n",
    "    (kind/table {:element/max-height \"300px\"}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9659433b-97c3-474d-b20d-c7a48c828e50",
   "metadata": {},
   "outputs": [],
   "source": [
    "(-> people-as-maps\n",
    "    tc/dataset\n",
    "    (kind/table {:use-datatables true}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3158a594-6cfd-429a-b088-3a5125494837",
   "metadata": {},
   "outputs": [],
   "source": [
    "(-> people-as-dataset\n",
    "    (kind/table {:use-datatables true}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28297d00-e919-492b-ba25-dd084f647969",
   "metadata": {},
   "outputs": [],
   "source": [
    "(-> people-as-dataset\n",
    "    (kind/table {:use-datatables true\n",
    "                 :datatables {:scrollY 200}}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aab1be7f-703a-47ad-807d-5e4e97e28af6",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/table\n",
    " {:column-names [(kind/code \":x\")\n",
    "                 (kind/code \":y\")]\n",
    "  :row-vectors [[(kind/md \"*some text* **some more text**\")\n",
    "                 (kind/code \"{:x (1 2 [3 4])}\")]\n",
    "                [(tc/dataset {:x (range 3)\n",
    "                              :y (map inc (range 3))})\n",
    "                 vega-lite-plot]\n",
    "                [(kind/hiccup [:div {:style {:height \"200px\"\n",
    "                                             :width \"200px\"}}\n",
    "                               tree-image])\n",
    "                 (kind/md \"**hello**\")]]})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29a2984a-4971-433a-80cc-b0ecd9f39e97",
   "metadata": {},
   "source": [
    "# :kind/pprint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93430b91-cb1a-4d24-93e5-7844a9b24f40",
   "metadata": {},
   "outputs": [],
   "source": [
    "(->> (range 30)\n",
    "     (apply array-map)\n",
    "     kind/pprint)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "34b914a3-334d-4e10-8875-0713038ecd1e",
   "metadata": {},
   "outputs": [],
   "source": [
    "(->> (range 10)\n",
    "     (apply array-map))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41ca2d70-a251-415b-9ab1-f57075acc505",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/pprint\n",
    " hello-hiccup)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc95692d-c560-4de5-8777-435ac221fe56",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/pprint\n",
    " tree-image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0c8e33cb-2b86-4697-9f52-33d7995ac070",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/pprint\n",
    " kind/dataset)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2cee76b7-5c7e-4e63-9502-972d1763a0a7",
   "metadata": {},
   "source": [
    "# :kind/portal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3fe5da4d-0b50-411f-8894-e56697e89071",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/portal\n",
    " {:x (range 3)})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a85269ca-3095-4d3a-afd8-1371b12a8673",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/portal\n",
    " [(kind/hiccup [:big [:big \"a plot\"]])\n",
    "  vega-lite-plot])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6afda7d5-9ac7-4e52-a0f4-4867f1973799",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/portal\n",
    " [(kind/hiccup [:p {:style {:background-color \"#ccddcc\"\n",
    "                            :border-style \"solid\"}}\n",
    "                \"hello\"])\n",
    "  (kind/md\n",
    "   \"hello *hello* **hello**\")\n",
    "  (kind/code\n",
    "   \"(defn f [x] (+  x 9))\")\n",
    "  vega-lite-plot])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34cac6e1-dff4-4043-a803-c9fbe7850428",
   "metadata": {},
   "source": [
    "# :kind/fragment\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16d72820-dea6-44c7-bbe1-0ffa5b285923",
   "metadata": {},
   "outputs": [],
   "source": [
    "(->> [\"purple\" \"darkgreen\" \"goldenrod\"]\n",
    "     (mapcat (fn [color]\n",
    "               [(kind/md (str \"### subsection: \" color))\n",
    "                (kind/hiccup [:div {:style {:background-color color\n",
    "                                            :color \"lightgrey\"}}\n",
    "                              [:big [:p color]]])]))\n",
    "     kind/fragment)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "311e0079-fdd2-42d1-be7a-b2f465560584",
   "metadata": {},
   "source": [
    "# :kind/fn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7736a4f-1564-454f-a66e-039d4b324901",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/fn {:x 1\n",
    "          :y 2}\n",
    "  {:kindly/f (fn [{:keys [x y]}]\n",
    "               (+ x y))})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ca0af454-e817-4ce8-a0dd-acb4f5df908e",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/fn\n",
    "  [+ 1 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46ed4283-f143-4a47-9085-f18ce7e6c2de",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/fn\n",
    "  {:kindly/f (fn [{:keys [x y]}]\n",
    "               (+ x y))\n",
    "   :x 1\n",
    "   :y 2})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ddd958f-3355-4cc7-9ddb-4fe04402c3d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/fn\n",
    "  {:x (range 3)\n",
    "   :y (repeatedly 3 rand)}\n",
    "  {:kindly/f tc/dataset})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a427373-0c45-4381-88f8-e76c0ed14f94",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/fn\n",
    "  [tc/dataset\n",
    "   {:x (range 3)\n",
    "    :y (repeatedly 3 rand)}])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb1ced05-4a17-447b-852d-da3c75bda3ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/fn\n",
    "  {:kindly/f tc/dataset\n",
    "   :x (range 3)\n",
    "   :y (repeatedly 3 rand)})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "293b632a-ebec-42fe-8a43-c813ba7f638e",
   "metadata": {},
   "source": [
    "# :kind/test-last"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7af2791d-4b22-41ef-a0fa-2b3a265e5eca",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/test-last)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc758c5e-c0bb-4dbd-bb92-44171636bfee",
   "metadata": {},
   "source": [
    "# :kind/test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "992d7fa4-8c47-47ef-bdbf-4197b59e1ef5",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/test)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92fa0065-8244-4796-9162-6ede2235e26b",
   "metadata": {},
   "source": [
    "# :kind/var"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71588554-2116-4767-8a4d-6676df5d4c6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/var\n",
    " '(def a \"hello\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0ff3ebb-6e06-4f03-a059-35ddd1105c8c",
   "metadata": {},
   "source": [
    "# :kind/emmy-viewers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9da4aa8b-e21e-4c8b-bf3a-3dba7e7a5641",
   "metadata": {},
   "outputs": [],
   "source": [
    "  (require\n",
    "   '[scicloj.kindly.v4.kind :as kind]\n",
    "   '[emmy.env :as e :refer :all #_[D square cube tanh cos sin up down]]\n",
    "   '[emmy.viewer :as ev]\n",
    "   '[emmy.mafs :as mafs]\n",
    "   '[emmy.mathbox.plot :as plot]\n",
    "   '[emmy.leva :as leva])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea3d1a18-bc4a-4efe-be57-267b4b494647",
   "metadata": {},
   "outputs": [],
   "source": [
    "(mafs/of-x e/sin {:color :blue})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8dba6930-c8cd-4be0-b793-e61a0a6a5054",
   "metadata": {},
   "source": [
    "# :kind/scittle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33c4872d-1cb0-4da5-932c-fca7310b3550",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/hiccup [:div {:id \"my_text\" } \"my text\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e012bff9-dd4a-4f66-a3d2-5f41d0c7a30f",
   "metadata": {},
   "outputs": [],
   "source": [
    "(kind/scittle '(-> js/document\n",
    "    (.getElementById \"my_text\")\n",
    "    (.-innerHTML)\n",
    "    (set! \"my MODIFIED text (via clojurescript)\"))) \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "444983bf-4e66-40aa-b9a0-173b71e4ef72",
   "metadata": {},
   "source": [
    "# :kind/htmlwidgets-ggplotly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "becb9839-fa57-46c6-8c53-5fad7efea345",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "(require '[clojure.math :as math])\n",
    "(defn ->ggplotly-spec [{:keys [layers labels]}]\n",
    "  (kind/htmlwidgets-ggplotly\n",
    "   (let [;; assuming a single layer for now:\n",
    "         {:keys [data xmin xmax ymin ymax]} (first layers)\n",
    "         ;; an auxiliary function to compute tick values:\n",
    "         ->tickvals (fn [l r]\n",
    "                      (let [jump (-> (- r l)\n",
    "                                     (/ 6)\n",
    "                                     math/floor\n",
    "                                     int\n",
    "                                     (max 1))]\n",
    "                        (-> l\n",
    "                            math/ceil\n",
    "                            (range r jump))))]\n",
    "     {:x\n",
    "      {:config\n",
    "       {:doubleClick \"reset\",\n",
    "        :modeBarButtonsToAdd [\"hoverclosest\" \"hovercompare\"],\n",
    "        :showSendToCloud false},\n",
    "       :layout\n",
    "       {:plot_bgcolor \"rgba(235,235,235,1)\",\n",
    "        :paper_bgcolor \"rgba(255,255,255,1)\",\n",
    "        :legend\n",
    "        {:bgcolor \"rgba(255,255,255,1)\",\n",
    "         :bordercolor \"transparent\",\n",
    "         :borderwidth 1.88976377952756,\n",
    "         :font {:color \"rgba(0,0,0,1)\", :family \"\", :size 11.689497716895}},\n",
    "        :xaxis (let [tickvals (->tickvals xmin xmax)\n",
    "                     ticktext (mapv str tickvals)\n",
    "                     range-len (- xmax xmin)\n",
    "                     range-expansion (* 0.1 range-len)\n",
    "                     expanded-range [(- xmin range-expansion)\n",
    "                                     (+ xmax range-expansion)]]\n",
    "                 {:linewidth 0,\n",
    "                  :nticks nil,\n",
    "                  :linecolor nil,\n",
    "                  :ticklen 3.65296803652968,\n",
    "                  :tickcolor \"rgba(51,51,51,1)\",\n",
    "                  :tickmode \"array\",\n",
    "                  :gridcolor \"rgba(255,255,255,1)\",\n",
    "                  :automargin true,\n",
    "                  :type \"linear\",\n",
    "                  :tickvals tickvals\n",
    "                  :zeroline false,\n",
    "                  :title\n",
    "                  {:text (:x labels),\n",
    "                   :font {:color \"rgba(0,0,0,1)\", :family \"\", :size 14.6118721461187}},\n",
    "                  :tickfont {:color \"rgba(77,77,77,1)\", :family \"\", :size 11.689497716895},\n",
    "                  :autorange false,\n",
    "                  :showticklabels true,\n",
    "                  :showline false,\n",
    "                  :showgrid true,\n",
    "                  :ticktext ticktext\n",
    "                  :ticks \"outside\",\n",
    "                  :gridwidth 0.66417600664176,\n",
    "                  :anchor \"y\",\n",
    "                  :domain [0 1],\n",
    "                  :hoverformat \".2f\",\n",
    "                  :tickangle 0,\n",
    "                  :tickwidth 0.66417600664176,\n",
    "                  :categoryarray ticktext,\n",
    "                  :categoryorder \"array\",\n",
    "                  :range expanded-range},)\n",
    "        :font {:color \"rgba(0,0,0,1)\", :family \"\", :size 14.6118721461187},\n",
    "        :showlegend false,\n",
    "        :barmode \"relative\",\n",
    "        :yaxis (let [tickvals (->tickvals ymin ymax)\n",
    "                     ticktext (mapv str tickvals)\n",
    "                     range-len (- ymax ymin)\n",
    "                     range-expansion (* 0.1 range-len)\n",
    "                     expanded-range [(- ymin range-expansion)\n",
    "                                     (+ ymax range-expansion)]]\n",
    "                 {:linewidth 0,\n",
    "                  :nticks nil,\n",
    "                  :linecolor nil,\n",
    "                  :ticklen 3.65296803652968,\n",
    "                  :tickcolor \"rgba(51,51,51,1)\",\n",
    "                  :tickmode \"array\",\n",
    "                  :gridcolor \"rgba(255,255,255,1)\",\n",
    "                  :automargin true,\n",
    "                  :type \"linear\",\n",
    "                  :tickvals tickvals,\n",
    "                  :zeroline false,\n",
    "                  :title\n",
    "                  {:text (:y labels),\n",
    "                   :font {:color \"rgba(0,0,0,1)\", :family \"\", :size 14.6118721461187}},\n",
    "                  :tickfont {:color \"rgba(77,77,77,1)\", :family \"\", :size 11.689497716895},\n",
    "                  :autorange false,\n",
    "                  :showticklabels true,\n",
    "                  :showline false,\n",
    "                  :showgrid true,\n",
    "                  :ticktext ticktext,\n",
    "                  :ticks \"outside\",\n",
    "                  :gridwidth 0.66417600664176,\n",
    "                  :anchor \"x\",\n",
    "                  :domain [0 1],\n",
    "                  :hoverformat \".2f\",\n",
    "                  :tickangle 0,\n",
    "                  :tickwidth 0.66417600664176,\n",
    "                  :categoryarray ticktext,\n",
    "                  :categoryorder \"array\",\n",
    "                  :range expanded-range},)\n",
    "        :hovermode \"closest\",\n",
    "        :margin\n",
    "        {:t 25.7412480974125,\n",
    "         :r 7.30593607305936,\n",
    "         :b 39.6955859969559,\n",
    "         :l 37.2602739726027},\n",
    "        :shapes\n",
    "        [{:yref \"paper\",\n",
    "          :fillcolor nil,\n",
    "          :xref \"paper\",\n",
    "          :y1 1,\n",
    "          :type \"rect\",\n",
    "          :line {:color nil, :width 0, :linetype []},\n",
    "          :y0 0,\n",
    "          :x1 1,\n",
    "          :x0 0}]},\n",
    "       :highlight\n",
    "       {:on \"plotly_click\",\n",
    "        :persistent false,\n",
    "        :dynamic false,\n",
    "        :selectize false,\n",
    "        :opacityDim 0.2,\n",
    "        :selected {:opacity 1},\n",
    "        :debounce 0},\n",
    "       :base_url \"https://plot.ly\",\n",
    "       :cur_data \"1f2fea5b54d146\",\n",
    "       :source \"A\",\n",
    "       :shinyEvents\n",
    "       [\"plotly_hover\"\n",
    "        \"plotly_click\"\n",
    "        \"plotly_selected\"\n",
    "        \"plotly_relayout\"\n",
    "        \"plotly_brushed\"\n",
    "        \"plotly_brushing\"\n",
    "        \"plotly_clickannotation\"\n",
    "        \"plotly_doubleclick\"\n",
    "        \"plotly_deselect\"\n",
    "        \"plotly_afterplot\"\n",
    "        \"plotly_sunburstclick\"],\n",
    "       :attrs {:1f2fea5b54d146 {:x {}, :y {}, :type \"scatter\"}},\n",
    "       :data\n",
    "       [{:y (:y data)\n",
    "         :hoveron \"points\",\n",
    "         :frame nil,\n",
    "         :hoverinfo \"text\",\n",
    "         :marker\n",
    "         {:autocolorscale false,\n",
    "          :color \"rgba(0,0,0,1)\",\n",
    "          :opacity 1,\n",
    "          :size 5.66929133858268,\n",
    "          :symbol \"circle\",\n",
    "          :line {:width 1.88976377952756, :color \"rgba(0,0,0,1)\"}},\n",
    "         :mode \"markers\"\n",
    "         :type \"scatter\",\n",
    "         :xaxis \"x\",\n",
    "         :showlegend false,\n",
    "         :yaxis \"y\",\n",
    "         :x (:x data)\n",
    "         :text (-> data\n",
    "                   (tc/select-columns [:x :y])\n",
    "                   (tc/rows :as-maps)\n",
    "                   (->> (mapv pr-str)))}]},\n",
    "      :evals [],\n",
    "      :jsHooks []})))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1059f624-5657-423b-a5d2-15c1a34e6705",
   "metadata": {},
   "outputs": [],
   "source": [
    "(require '[tablecloth.column.api :as tcc])\n",
    "(let [n 100\n",
    "      xs (range n)\n",
    "      ys (reductions + (repeatedly n #(- (rand) 0.5)))\n",
    "      xmin (tcc/reduce-min xs)\n",
    "      xmax (tcc/reduce-max xs)\n",
    "      ymin (tcc/reduce-min ys)\n",
    "      ymax (tcc/reduce-max ys)\n",
    "      data (tc/dataset {:x xs\n",
    "                        :y ys})]\n",
    "  (->ggplotly-spec\n",
    "   {:layers [{:data data\n",
    "              :xmin xmin :xmax xmax\n",
    "              :ymin ymin :ymax ymax}]\n",
    "    :labels {:x \"wt\"\n",
    "             :y \"mpg\"}}))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40114af5-5185-403a-957b-3cc3c4f074de",
   "metadata": {},
   "source": [
    "## nesting in kind/set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b86eb59-9429-4f28-9736-cd8935029410",
   "metadata": {},
   "outputs": [],
   "source": [
    "#{tree-image vega-lite-plot m  tex }\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63a748be-4c3d-4e53-8da6-d24092eb19e5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Clojure (clojupyter-0.5.424-snapshot424)",
   "language": "clojure",
   "name": "clojupyter-0.5.424-snapshot424"
  },
  "language_info": {
   "file_extension": ".clj",
   "mimetype": "text/x-clojure",
   "name": "clojure",
   "version": "1.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
